

package com.mt.mall.controller.good;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.good.EvaluationEditDto;
import com.mt.mall.entity.good.Evaluation;
import com.mt.mall.service.good.EvaluationService;
import com.mt.common.system.service.UserService;
import com.mt.mall.service.good.EvaluationService;
import com.mt.mall.service.good.CommodityService;
import com.mt.mall.service.Order.OrderService;


@Api(tags = "商品评价")
@RestController
@RequestMapping("/api/mall/good/Evaluation")
@CrossOrigin(allowCredentials = "true")
public class EvaluationController {
    private static Logger logger = LoggerFactory.getLogger(EvaluationController.class);


    @Autowired
    private EvaluationService evaluationService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private OrderService orderService;

    /**
     * 根据分页参数查询商品评价集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("分页查询商品评价")
    @ApiPageParam
    @PostMapping("/findEvaluations")
    public PageResultDTO findEvaluations(@RequestBody PageDTO pageDTO) {
        return this.evaluationService.findEvaluations(pageDTO);
    }

    /**
     * 根据ID查询指定的商品评价
     *
     * @param evaluationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("根据id查询商品评价")
    @ApiPageParam
    @PostMapping("/findEvaluation")
    public Evaluation findEvaluation(@RequestParam Long evaluationId) {
        return this.evaluationService.findEvaluation(evaluationId);
    }

    /**
     * 根据ID查询指定的商品评价(包含外键名称)
     *
     * @param evaluationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("根据ID查询指定的商品评价(包含外键名称)")
    @PostMapping("/findEvaluationForView")
    public Evaluation findEvaluationForView(@RequestParam Long evaluationId) {
        return this.evaluationService.findEvaluationWithForeignName(evaluationId);
    }

    /**
     * 根据ID查询指定的商品评价(包含商品评价和外键名称)
     *
     * @param evaluationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("根据ID查询指定的商品评价(包含商品评价和外键名称)")
    @PostMapping("/findEvaluationForEdit")
    public EvaluationEditDto findEvaluationForEdit(@RequestParam Long evaluationId) {
        EvaluationEditDto evaluationEditDto = new EvaluationEditDto();
        evaluationEditDto.setEvaluation(this.evaluationService.findEvaluationWithForeignName(evaluationId));

        this.prepareEvaluationEditDto(evaluationEditDto);

        return evaluationEditDto;
    }

    /**
     * 根据ID查询指定的商品评价(只提取ID 和 Name)
     *
     * @param evaluationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("根据ID查询指定的商品评价(只提取ID 和 Name)")
    @PostMapping("/findEvaluationsWithIdNameById")
    public Evaluation findEvaluationsWithIdNameById(@RequestParam Long evaluationId) {
        return this.evaluationService.findEvaluationsWithIdNameById(evaluationId);
    }

    /**
     * 根据名称查询商品评价集合(只提取ID 和 Name)
     *
     * @param evaluationName 名称
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:view')")
    @ApiOperation("根据名称查询商品评价集合(只提取ID 和 Name)")
    @PostMapping("/findEvaluationsWithIdNameByName")
    public List<Evaluation> findEvaluationsWithIdNameByName(@RequestParam String evaluationName) {
        return this.evaluationService.findEvaluationsWithIdNameByName(evaluationName);
    }


    /**
     * 创建新的商品评价
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:add')")
    @ApiOperation("创建新的商品评价")
    @PostMapping("/createEvaluation")
    public EvaluationEditDto createEvaluation() {
        EvaluationEditDto evaluationEditDto = new EvaluationEditDto();
        evaluationEditDto.setEvaluation(new Evaluation());

        this.prepareEvaluationEditDto(evaluationEditDto);
        return evaluationEditDto;
    }

    private void prepareEvaluationEditDto(EvaluationEditDto evaluationEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        evaluationEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        evaluationEditDto.setEvaluationsEvaluations(this.evaluationService.findAllEvaluationsWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        evaluationEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        evaluationEditDto.setOrderOrders(this.orderService.findAllOrdersWithIdName());
    }

    /**
     * 新增保存商品评价
     *
     * @param evaluation 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:add')")
    @ApiOperation("新增保存商品评价")
    @PostMapping("/saveEvaluation")
    public Evaluation saveEvaluation(@RequestBody Evaluation evaluation) {
        return this.evaluationService.saveEvaluation(evaluation);
    }

    /**
     * 修改保存商品评价
     *
     * @param evaluation 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:update')")
    @ApiOperation("修改保存商品评价")
    @PostMapping("/updateEvaluation")
    public Evaluation updateEvaluation(@RequestBody Evaluation evaluation) {
        return this.evaluationService.updateEvaluation(evaluation);
    }

    /**
     * 根据ID删除商品评价
     *
     * @param evaluationId ID
     */
    @PreAuthorize("hasAuthority('mall:good:Evaluation:remove')")
    @ApiOperation("根据ID删除商品评价")
    @PostMapping("/deleteEvaluation")
    public void deleteEvaluation(@RequestParam Long evaluationId) {
        this.evaluationService.deleteEvaluation(evaluationId);
    }


}

